// packageA/pages/cashService/operator/login/login.js
const {
  imgPath
} = require('../../../utils/request')
const {
  request
} = require('../../../utils/request');
const {
  hexMD5
} = require('../../../utils/md5');
const {
  encryptSM2
} = require('../../../utils/sm-crypto');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: imgPath,
    captchaData: {},
    loginMode: 2, // 1 账号登录 2密码登录
    idNum: '',  //202309整改，字段含义改为操作员账号
    phone: '',
    captcha: '',
    smsCaptcha: '',

    btnText: '获取验证码',
    btnDisable: false,
    timeout: 0,
    username: '',
    password: '',
    repeatPassword: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '用户密码重置'
    })
    this.fetchCaptcha()
  },
  onShow: function () {
   
  },

  /**
   * 获取图片验证码
   */
  fetchCaptcha() {
    request('/smsk/wechat/auth/captchaw150h30', 'GET').then(res => {
      this.setData({
        captchaData: res.data.data,
      })
    })
  },

  /**
   * 获取短信验证码
   */
  fetchSmsCaptcha() {
    // 校验表单
    if (!this.validForm()) {
      return
    }
    // 拼装参数
    let query = {
      idNum: encryptSM2(this.data.idNum, this.data.captchaData.publicKey),
      // phone: this.data.phone,
      captchaId: this.data.captchaData.captchaId,
      captcha: this.data.captcha
    }
    request('/smsk/wechat/auth/resetSms', 'POST', query).then(res => {
      console.log(res.data)
      if (res.data.code == 0) {
        wx.showToast({
          title: '短信发送成功',
          icon: 'success'
        })
        this.setData({
          btnDisable: true,
          timeout: 300
        })
        this.btnCountDown()
      } else {
        this.showToast(res.data.message)
        this.fetchCaptcha()
      }
    })
  },
  /**
   * 修改短信发送按钮状态
   */
  btnCountDown() {
    if (this.data.timeout === 0) {
      this.setData({
        btnDisable: false,
        btnText: '获取验证码'
      })
    } else {
      this.setData({
        btnText: '重新发送(' + this.data.timeout + ')'
      })
      setTimeout(() => {
        this.setData({
          timeout: this.data.timeout - 1,
        })
        this.btnCountDown()
      }, 1000)
    }
  },

  /**
   * 点击“立即绑定”
   */
  login() {
    // 表单校验
    if (!this.validForm('login')) {
      return
    }
    // 防止重复点击登录
    if (this.data.loginDisable) {
      return
    }
    this.setData({
      loginDisable: true
    })
     // 拼装参数
     let loginData = {
      idNum: encryptSM2(this.data.idNum, this.data.captchaData.publicKey),
      newpasswd: encryptSM2(this.data.password, this.data.captchaData.publicKey),
      captcha: this.data.captcha,
      captchaId: this.data.captchaData.captchaId,
      smsCaptcha: this.data.smsCaptcha
    }
    wx.showLoading({
      title: '重置中',
    })
    console.log(loginData)
    request('/smsk/wechat/auth/resetPasswd', 'POST', loginData).then(res => {
      console.log(res.data)
      this.setData({
        loginDisable: false
      })
      if (res.data.code == 0) {
        // 重置成功后返回原先页面
        wx.navigateBack()
      } else {
        this.showToast(res.data.message)
        this.fetchCaptcha()
      }
    })
  },
  /**
   * 表单校验
   */
  validForm(str) {
      if (!this.data.idNum) {
        this.showToast('请填写用户账号')
        return false
      }
      // if (!this.data.phone || !(/^1[345789]\d{9}$/.test(this.data.phone))) {
      //   this.showToast('请填写正确的手机号')
      //   return false
      // }
      if (!this.data.password) {
        this.showToast('请填写新密码')
        return false
      }
      if (!this.data.repeatPassword) {
        this.showToast('请填写确认新密码')
        return false
      }
      if (this.data.password !== this.data.repeatPassword) {
        this.showToast('两次密码不一致，请确认')
        return false
      }
      if (!this.data.captcha || !(this.data.captchaData.hashCode == hexMD5(this.data.captcha.toUpperCase()))) {
        this.showToast('请填写正确的图片验证码')
        return false
      }
      if (str == 'login') {
        if (!this.data.smsCaptcha) {
          this.showToast('请填写正确的短信验证码')
          return false
        }
      }
      return true
  },

  /**
   * 简化弹出
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },
  /**
   * 切换登录模式
   */
  toggleLoginMode() {
    if (this.data.loginMode === 1) {
      this.setData({
        loginMode: 2
      });
    } else {
      this.setData({
        loginMode: 1
      });
    }

  }
})